package de.melistik.smugmugsync;

import java.awt.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import de.melistik.IconHandler;
import de.melistik.smugmugsync.model.SyncFolder;

@SuppressWarnings("serial")
public class SyncFolderTableModel extends AbstractTableModel {

	private String[] columnNames = { "Sync", "Folder", "Category", "Exist",
			"Album", "Exist", "Sub", "FilesInFolder", "onSmugMug" };
	private Integer[] columnWidth = { 40, null, 150, 40, null, 40, 50, 85, 85 };
	private Boolean[] imageRenderer = { false, false, false, true, false, true,
			false, false, false };
	private Integer[] columnAlign = { null, SwingConstants.LEFT,
			SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.LEFT,
			SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.RIGHT,
			SwingConstants.RIGHT };

	List<SyncFolder> syncFolderList = new ArrayList<SyncFolder>();
	File startSyncFolder;

	public SyncFolderTableModel() {
	}

	public SyncFolderTableModel(File startSyncFolder) {
		this.startSyncFolder = null;
	}

	public List<SyncFolder> getSyncFolderList() {
		return syncFolderList;
	}

	public void setSyncFolderList(List<SyncFolder> syncFolderList) {
		this.syncFolderList = syncFolderList;
	}

	public File getStartSyncFolder() {
		return startSyncFolder;
	}

	public void setStartSyncFolder(File startSyncFolder) {
		this.startSyncFolder = startSyncFolder;
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return syncFolderList.size();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		Object result = null;
		if (syncFolderList.size() > row) {
			SyncFolder syncFolder = syncFolderList.get(row);
			switch (col) {
			case 0:
				result = syncFolder.isSyncEnabled();
				break;
			case 1:
				result = SyncFolder.getShortFolderPath(startSyncFolder,
						syncFolder.getFolder());
				break;
			case 2:
				result = syncFolder.getCategoryName();// syncFolder.getFirstFolderInShortPath(startSyncFolder);
				break;
			case 3:
				if (syncFolder.getCategoryId() == null) {
					result = null;
				} else {
					if (syncFolder.getCategoryId().intValue() <= 0) {
						result = IconHandler.cross;
					} else {
						result = IconHandler.accept;
					}
				}
				break;
			case 4:
				result = syncFolder.getAlbumName(); // syncFolder.getFoldersAfterFirstInShortPath(startSyncFolder);
				break;
			case 5:
				if (syncFolder.getAlbumId() == null) {
					result = null;
				} else {
					if (syncFolder.getAlbumId().intValue() <= 0) {
						result = IconHandler.cross;
					} else {
						result = IconHandler.accept;
					}
				}
				break;
			case 6:
				result = syncFolder.getSubFolders().size();
				break;
			case 7:
				result = syncFolder.getImagesInFolder().size();
				break;
			case 8:
				result = syncFolder.getFilesOnSmugMug();
				break;
			default:
				result = 0;
				break;
			}
		}
		return result;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Class getColumnClass(int col) {
		Class result = null;
		switch (col) {
		case 0:
			result = Boolean.class;
			break;
		case 4:
			result = Integer.class;
			break;
		default:
			result = String.class;
			break;
		}
		return result;
	}

	public boolean isCellEditable(int row, int col) {
		return col == 0;
	}

	public void setValueAt(Object value, int row, int col) {
		if (syncFolderList.size() > row) {
			SyncFolder syncFolder = syncFolderList.get(row);
			switch (col) {
			case 0:
				syncFolder.setSyncEnabled((Boolean) value);
				break;
			}
		}
	}

	static class ImageRenderer extends DefaultTableCellRenderer {
		JLabel lbl = new JLabel();

		public ImageRenderer() {
			super();
			setHorizontalTextPosition(SwingConstants.CENTER);
		}

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			if (value instanceof ImageIcon) {
				lbl.setIcon((ImageIcon) value);
				return lbl;
			} else {
				return super.getTableCellRendererComponent(table, value,
						isSelected, hasFocus, row, column);
			}
		}
	}

	public void initColumnSizes(JTable table) {
		TableColumn column = null;
		for (int i = 0; i < columnWidth.length; i++) {
			column = table.getColumnModel().getColumn(i);
			if (columnWidth[i] != null) {
				column.setPreferredWidth(columnWidth[i]);
				column.setMaxWidth(columnWidth[i]);
			}
			if (imageRenderer[i]) {
				column.setCellRenderer(new ImageRenderer());
			} else {
				if (columnAlign[i] != null) {
					column.setCellRenderer(new DefaultTableCellRenderer());
					((DefaultTableCellRenderer) column.getCellRenderer())
							.setHorizontalTextPosition(columnAlign[i]);
				}
			}
		}
	}
}
